<?php include_once ('function/check_login_function.php'); onlyValid();?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Health Tracker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Health Tracker">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="shortcut icon" href="ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>
    <?php include_once('nav.php'); ?>
    
    <div class="container">
    <div class="row">
    <div class="span8">
      <h1>Doctors</h1>
		<?php
			if ($user->getRole() === 'doctor') {
			if (intval($user->getVerified()) === intval(false)) {
				echo '<p class="text-error">' . constant("ERROR_DOCTOR_UNVERIFIED") . '</p>';
			}
		}
		?>
	  
        <div class="alert alert-info">
        <form action="doctors.php" method="POST" class="form-inline">
        <p>Search for doctors given their names, speciality or hospital name</p>
        <label for="filter">Search:</label> <input type="text" id="filter" name="filter"/>
        <button type="submit" name="filterDoctor" class="btn btn-primary">Go</button>
        </form>	
        </div>
						<?php
							if (isset($_POST['hospitalID'])) {
								$hospital_id = $_POST['hospitalID'];
								$doctor_list = Hospital::getDoctorList($hospital_id);
								echo '<h3>' . Hospital::findName($hospital_id) . '</h3>';
							} else if (isset($_POST['filter'])){
								$doctor_list = Doctor::searchDoctor($_POST['filter']);
								echo '<h3> Filter for ' . $_POST['filter'] . '</h3>';
							} else {
								$doctor_list = Doctor::searchDoctor();
								echo '<h3> Show All </h3>';
							}
							if ($doctor_list !== null) {
								$id_list = Patient::doctorIDList($user->getUserID());
								foreach ($doctor_list as $doctor) {
									echo '<div class="well"><table>';
									echo '<tr><td><b>Name:</b></td><td><b>' . $doctor['user_firstname'] . ' ' . $doctor['user_lastname'] . '</b></td></tr>';
									echo '<tr><td>Gender:</td><td>' . $doctor['user_gender'] . '</td></tr>';
									echo '<tr><td>E-mail:</td><td>' . $doctor['user_email'] . '</td></tr>';
									echo '<tr><td>Contact:</td><td>' . $doctor['user_contact'] . '</td></tr>';
									echo '<tr><td>Speciality:</td><td>' . $doctor['doctor_speciality'] . '</td></tr>';
									echo '<tr><td>Registration No.:</td><td>' . $doctor['doctor_regno'] . '</td></tr>';
								
									$hospital_list = Doctor::findHospitalList($doctor['user_id']);
									if ($hospital_list !== null) {
										foreach ($hospital_list as $hospital) {
											echo '<tr><td>Hospital:</td><td>' . $hospital['hospital_name'] . '</td></tr>';
										}
									}
							
									echo '</table>';
									if ($user->getRole() === 'patient') {
										echo '<form action="request.php" method="POST">';
										echo '<input type="hidden" id="doctorID" name="doctorID" value="' . $doctor['user_id'] . '"/>';
										echo '<input type="hidden" id="from" name="from" value="doctors.php"/>';
										echo '<button type="submit" name="selectDoctor" class="btn btn-primary">Select</button>';
										echo '</form>';
									}
									
									if ($id_list !== null && in_array($doctor['user_id'], $id_list)) {
										echo 'You have already sent a request to this doctor.';
										echo '<form action="function/form_function.php" method="POST">';
										echo '<input type="hidden" id="doctorID" name="doctorID" value="' . $doctor['user_id'] . '"/>';
										echo '<input type="hidden" id="from" name="from" value="doctors.php"/>';
										echo '<input type="submit" name="removeDoctorRequest" value="Remove Request" class="btn btn-primary"/> ';
										echo' </form>';										
									}
									echo '</div>';
								}
							} else {
								echo 'No doctors match the criteria' ;
							}
						?>
    </div>
    </div>
    </div> <!-- /container -->
  </body>
</html>